package com.muqi.app.im.adapter;

import java.util.List;

import com.bumptech.glide.Glide;
import com.easemob.easeui.widget.CircularImage;
import com.muqi.app.qlearn.modles.PersonBean;
import com.muqi.app.qlearn.student.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class SearchFriendsAdapter extends BaseAdapter {
	private Context context;
	private List<PersonBean> listData;
	private AddFriendsListener listener;

	public SearchFriendsAdapter(Context context, List<PersonBean> listData,
			AddFriendsListener listener) {
		this.context = context;
		this.listData = listData;
		this.listener = listener;
	}

	@Override
	public int getCount() {
		return listData == null ? 0 : listData.size();
	}

	@Override
	public PersonBean getItem(int position) {
		return listData.get(position);
	}
   
	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final PersonBean friendInfo = listData.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_search_friends, null);
			holder.friendAvatar = (CircularImage) convertView.findViewById(R.id.friends_circularImage);
			holder.friendName = (TextView) convertView.findViewById(R.id.friends_name);
			holder.addButton = (Button) convertView.findViewById(R.id.friends_add_button);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		Glide.with(context).load(friendInfo.avatarInfo.fileurl_02).dontAnimate()
		.placeholder(R.drawable.default_avator).into(holder.friendAvatar);
		holder.friendName.setText(friendInfo.nickname);
		holder.addButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				listener.onAddThisFriend(position);
			}
		});
		return convertView;
	}

	public interface AddFriendsListener {
		void onAddThisFriend(int position);
	}
	
	private class ViewHolder {
		CircularImage friendAvatar;
		TextView friendName;
		Button addButton;
	}
}
